home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 24 / Amiga Format AFCD24 (Feb 1998, Issue 108).iso / -in_the_mag- / emulation / -otherstuff- / amiganpi / src_c / main.c < prev    next >
C/C++ Source or Header  |  1998-01-05  |  3KB  |  137 lines

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <sys/stat.h>
  4. #include <termios.h>
  5.  
  6. #define LR  1
  7. #define LD  2
  8. #define LT  4
  9. #define LA  5
  10. #define LN  6
  11. #define LNA 7
  12.  
  13. sendLA(unsigned char nr,unsigned char cr) {
  14.     char la[] = {3,5,0,0};
  15.     la[2]=nr;
  16.     la[3]=cr;
  17.     send_frame(la, sizeof(la));
  18. }
  19.  
  20. unsigned char s;
  21. unsigned char r;
  22. FILE* newton;
  23.  
  24. int
  25. sendData(char *data, int size) {
  26.     unsigned char buf[2048];
  27.     unsigned char buf2[8];
  28.     int i;
  29.  
  30.     buf[0]=2;
  31.     buf[1]=4;
  32.     buf[2]=s;
  33.  
  34.     for(i=0;i<size;i++)
  35.         buf[i+3]=data[i];
  36.  
  37.     for(;;)    {
  38.         send_frame(buf,size+3);
  39.         read_frame(buf2,sizeof(buf2));
  40.         if(buf2[0]==3 && buf2[1]==5 && buf2[2]==s) {
  41.             s++;
  42.             return size;
  43.         }
  44.     }
  45. }
  46.  
  47. int
  48. receiveData(char *data, int size) {
  49.     char buf[2048];
  50.     int s,i;
  51.  
  52.     for(;;) {
  53.         s=read_frame(buf);
  54.         if(s>0) {
  55.             sendLA(r++,1);
  56.             s=s>size?size:s;
  57.             for(i=0;i<s;i++)
  58.                 data[i]=buf[i+3];
  59.             return s;
  60.         }
  61.     }
  62. }
  63.  
  64. int
  65. waitConnection() {
  66.     char lr[]={23,1,2,1,6,1,0,0,0,0,255,2,1,2,3,1,1,4,2,64,0,8,1,3};
  67.     char buf[64];
  68.  
  69.     while(read_frame(buf)<0);
  70.     send_frame(lr, sizeof(lr));
  71.     r=1;
  72.     read_frame(buf);
  73.     s=1;
  74. }
  75.  
  76. int
  77. sendDisconnect() {
  78.     char ld[]={7,2,1,1,255,2,1,0};
  79.     send_frame(ld, sizeof(ld));
  80. }
  81.  
  82. main(int argc, char* argv[]) {
  83.     int i,j;
  84.     int fd,nw,r;
  85.     unsigned int size;
  86.     struct stat st;
  87.     struct termios ts;
  88.     char* package;
  89.     char lt[]={'n','e','w','t','d','o','c','k','d','o','c','k',0,0,0,4,0,0,0,4};
  90.     char lt2[]={'n','e','w','t','d','o','c','k','s','t','i','m',0,0,0,4,0,0,0,5};
  91.     char lt3[]={'n','e','w','t','d','o','c','k','l','p','k','g',0,0,0x2f,0xa0};
  92.     char lt4[]={'n','e','w','t','d','o','c','k','d','i','s','c',0,0,0,0};
  93.     char buf[128];
  94.  
  95.     nw=open("/dev/newton",O_RDWR);
  96.     if(nm==0) {
  97.         fprintf(stderr,"Cannot open /dev/newton\n");
  98.         exit(-1);
  99.     }
  100.     cfmakeraw(&ts);
  101.     cfsetospeed(&ts,B115200);
  102.     cfsetispeed(&ts,B115200);
  103.     tcsetattr(nw,TCSANOW,&ts);
  104.     newton = fdopen(nw,"r+");
  105.     
  106.     fd=open(argv[1],O_RDONLY);
  107.     fstat(fd,&st);
  108.     (unsigned long)*(lt3+12)=htonl(st.st_size);
  109.  
  110.     fprintf(stderr,"Waiting...");
  111.     waitConnection();
  112.     fprintf(stderr,"\rConnecting...");
  113.     receiveData(buf,sizeof(buf));    // newtdockrtdk
  114.     sendData(lt,sizeof(lt));    // newtdockdock
  115.     receiveData(buf,sizeof(buf));    // newtdockname
  116.     sendData(lt2,sizeof(lt2));    // newtdockstim
  117.     receiveData(buf,sizeof(buf));    // newtdockdres
  118.     fprintf(stderr,"\rLoading %s, %d bytes\n",argv[1],st.st_size);
  119.     sendData(lt3,sizeof(lt3));    // newtdocklpkg
  120.  
  121.     r=read(fd,buf,sizeof(buf));
  122.     fprintf(stderr,"\r%d",size=r);
  123.     sendData(buf,r);
  124.     while(r==sizeof(buf)) {
  125.         r=read(fd,buf,sizeof(buf));
  126.         fprintf(stderr,"\r%d",size+=r);
  127.         sendData(buf,r);
  128.     }
  129.  
  130.     receiveData(buf,sizeof(buf));    // newtdockdres
  131.  
  132.     sendData(lt4,sizeof(lt4));    // newtdockdisc
  133.     sendDisconnect();
  134.     fprintf(stderr,"\n");
  135.     fflush(stderr);
  136. }
  137.